﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace Sync.Service
{
    class Program
    {


        static void Main(string[] args)
        {
            Console.WriteLine("----------------------------------------------------start.-----------------------------------------------------");
          
            Com();
            Console.WriteLine("----------------------------------------------------done.-----------------------------------------------------");
            Console.ReadKey();
        }



        static string PathExecl
        {
            get
            {
                //return "D:\\Repos\\Sync.Service\\Sync.Service\\组团单体信息.xls";
                return AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "附件信息.xls";
            }
        }
        static void Com()
        {

            var domain = new List<DomainInfo>() {
             
             new DomainInfo (){ Domain="https://bingo-test.longhu.net/",Authorization="Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9zaWQiOiI0YzU4OTVmZS05ZmI1LTQ4YzktYmI3Zi01MjRlMGJjOWIzMWYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6Imx1eWFuMyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiJsdXlhbjMiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9lbWFpbGFkZHJlc3MiOiJsdXlhbjNAbG9uZ2Zvci5jb20iLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9tb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iUmVhbE5hbWUiOiLmnY7lv5flhYkiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOlsiQXV0aG9yaXplZFVzZXJzIiwiVXNlcnMiLCJBZG1pbmlzdHJhdG9ycyIsIlByb2ZpdFJlcG9ydFVzZXJzIiwiRGFpbHlQcm9ncmVzc1JlcG9ydFVzZXJzIiwiTGlmZXRpbWVSaXNrTWVldGluZyIsIkdyb3VwQ29tcGxhaW50TGlzdFVzZXJzIiwiQ29tcGxhaW50SW1wb3J0YW50Tm9kZUFkbWluIiwiRnVsbFBlcmlvZFN0b3BSZXBvcnRVc2VycyIsIkZ1bGxQZXJpb2RSaXNrUmVwb3J0VXNlcnMiLCJGdWxsUGVyaW9kVGFza1JlcG9ydFVzZXJzIiwiTm9kZUNvbXBhcmVEZXRhaWxSZXBvcnRVc2VycyIsIk5vZGVDb21wYXJlVHJhY2tpbmdSZXBvcnRVc2VycyIsIkJpZ0RhdGFCdWlsZGluZ0NhcGFjaXR5VXNlcnMiLCJSaXNrTWVldGluZ1JlcG9ydFJhbmsxVXNlcnMiLCJSaXNrTWVldGluZ1JlcG9ydFJhbmsyVXNlcnMiLCJDbGFpbU1hbmFnZXJzIl0sIm5iZiI6MTYyMzI5NTAxMywiZXhwIjoxNjIzMzgxNDEzLCJpc3MiOiJMT05HRk9SX1JJU0siLCJhdWQiOiJMT05HRk9SX1JJU0sifQ.-5i9ykUjK-BU2TvHF5R5CmCYJMtzzud8q40mJdDJ9V0" }
            };

            var uploadUrl = "http://localhost:5000/api/Tools/ytxAUpload";
            var urls = new string[] { "file/downloadasync?fileId=${fileId}" };

            var table = ExcelHelper.ExcelToDS(PathExecl);
            List<CompareInfo> list = new List<CompareInfo>();
            var index = 0;
            foreach (DataRow row in table.Tables[0].Rows)
            {
                index++;
                var compare = new CompareInfo();
                compare.FileId = row["Id"].ToString();
                compare.FileExtension = row["fileExtension"].ToString();
                var url = domain[1].Domain + urls[0].Replace("${fileId}", compare.FileId);
                var header = new Dictionary<string, string>();
                header.Add("authorization", domain[0].Authorization);
                var parameters = new Dictionary<string, string>();
                try
                {
                    var stream = HttpHelper.HttpResponseStream("GET", url, header, parameters, "image/png");
                    //扩展名需要处理一下
                    FileStream fsw = new FileStream($"{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}{compare.FileId}.{compare.FileExtension}", FileMode.OpenOrCreate);
                    //写入
                    fsw.Write(stream.ToArray(), 0, stream.ToArray().Length);
                    //关闭文件流
                    fsw.Close();

                    ExcelHelper.UpdateFileStatus(compare.FileId, "1", PathExecl);
                    var files = new string[] { $"{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}{compare.FileId}.{compare.FileExtension}" };
                    HttpHelper.HttpUploadFile(uploadUrl, files ,null,Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    ExcelHelper.UpdateFileStatus(compare.FileId, "2", PathExecl);
                }
                Thread.Sleep(1000 * 2);
            }

            var NotList = list.Where(w => !w.IsEquals).ToList();
            var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            foreach (var info in NotList)
            {
                var p = path + "" + info.GroupMemberID + "-" + info.BuildingMemberID + ".txt";
                System.IO.File.AppendAllText(p, JsonConvert.SerializeObject(info));
            }
        }


        public static Stream DownloadFile(string url, string userToken = null)
        {
            // 获取下载地址
            var downloadUrl = url;
            HttpClient httpClient = new HttpClient();
            return httpClient.GetStreamAsync(downloadUrl).GetAwaiter().GetResult();
        }

        static void RemoveC(List<CompareInfo> list)
        {
            list.RemoveAll(w => w.IsEquals);
        }


        public class CompareInfo
        {
            public CompareInfo()
            {
                Json = new List<CompareResultInfo>();
            }

            public string FileId { get; set; }

            public string FileExtension { get; set; }
            public string GroupMemberID { get; set; }
            public string BuildingMemberID { get; set; }
            public bool IsEquals { get; set; }

            public List<CompareResultInfo> Json { get; set; }

            public void Compare()
            {
                IsEquals = false;
                if (Json.Count == 2)
                {
                    IsEquals = JToken.DeepEquals(JObject.Parse(Json[0].Json), JObject.Parse(Json[1].Json));
                    ExcelHelper.Update(GroupMemberID, BuildingMemberID, IsEquals ? "1" : "2", PathExecl);
                }
            }
        }

        public class CompareResultInfo
        {
            public string Json { get; set; }
            public string Domain { get; set; }

        }

        public class DomainInfo
        {
            public string Domain { get; set; }
            public string Authorization { get; set; }
        }



    }
}
